
########################
# Creating Bar Graph #
########################

## Load packages ##
library(readxl)
library(ggplot2) 
library(ggalt)   
library(tidyverse)
library(gridExtra)
library(RColorBrewer)

## Read data ##   # For automatation of code across differen datasets (weq, non-Asia etc) see earlier version of code
R_import_wealth_Asia_WPOP <- read_excel("[paste path]/Wealth_R import_Asia.xls", sheet = "wpop")

pal <- brewer.pal(9, "YlGnBu")
mid_pal <- pal[2:6]

## Create forest plot ##
Plot <- ggplot(R_import_wealth_Asia_WPOP, aes(
  y = glp1_elig_prev, 
  x = factor(countryGDPclass, levels = c("LIC", "Lower-MIC", "Upper-MIC", "HIC")), 
  fill = factor(wealth_quintile, levels = c("Poorest", "Poorer", "Middle", "Wealthier", "Wealthiest"))
)) +
  geom_bar(stat = "identity", position = position_dodge(width = 0.7), width = 0.6) +  # Bars with dodge for wealth quintiles
  geom_errorbar(
    aes(
      ymin = glp1_elig_lcl, 
      ymax = glp1_elig_ucl
    ), 
    position = position_dodge(width = 0.7),  # Match dodge width of bars
    width = 0.2,  size = 0.5, color ="grey35" 
  ) +
  labs(x = "World Bank country-income group", y = "GLP-1 receptor agonist eligibility (%)", fill = "Wealth quintile") +
  scale_fill_manual(values=mid_pal) +
  theme_classic() +
  theme(
    panel.grid.major.y = element_line(color = "grey80", size = 0.25),  # Horizontal gridlines
    axis.text.x = element_text(color = "black", size = 10),
    axis.text.y = element_text(color = "black", size = 11),
    legend.position = c(0.8, 0.8),
    legend.background = element_rect(color = "black", size = 0.3)
  ) +
  scale_y_continuous(
    limits = c(0, 100), 
    breaks = seq(0, 100, by = 10), 
    expand = expansion(add = c(0, 1))
  )  # Set y-axis from 0 to 100

ggsave("Bar graph_wpop_Asia.pdf", plot = Plot, width = 7, height = 8)
